var express = require('express');
var router = express.Router();
var fs = require("fs");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.locals.loginname = req.session.loginname || "";
  console.log(res.locals.loginname);
  var fslist1 = fs.readdirSync("fs");
  console.log(fslist1);
  var fsreturnlist = [] ;
  for(var i = 0 ; i < fslist1.length ; i ++){
    var content  = fs.readFileSync("fs/"+fslist1[i]);

    var fsObj  = {"bt":fslist1[i].split("_")[0],"lyr":fslist1[i].split("_")[1].replace(".txt",""),"nr":content.toString()}
    fsreturnlist.push(fsObj);
  }
  res.locals.fsreturnlist = fsreturnlist;
  console.log(res.locals.fsreturnlist);
  res.render('index');
});

router.post('/ly',function(req,res,next){
  const {bt,nr} = req.body;
  fs.appendFileSync("fs/"+bt+"_"+req.session.loginname+".txt",nr);
  res.redirect("back");
});
module.exports = router;
